from DrissionPage import ChromiumPage
import requests
import os




# 视频
if not os.path.exists('video'):
    os.mkdir('video')
# 图片
if not os.path.exists('img'):
    os.mkdir('img')
# 封面图片
if not os.path.exists('cover'):
    os.mkdir('cover')
# 视频图片
if not os.path.exists('video_img'):
    os.mkdir('video_img')





# 视频
def down_images(al, img_c):
    # 纯图片
    for i in al:
        # 图片
        try:
            images_url = i['images']
            for image in images_url:
                images_url = image['url_list'][-1]
                print('======================================')
                print(images_url)
                print('======================================')
                images_res = requests.get(images_url, headers=headers)
                with open(f'img/{img_c}.jpeg', 'wb') as f:
                    f.write(images_res.content)
                img_c += 1
        except:
            print('======================================')
            print('没有图片')
            # pass

    # pass
    # aweme_list[19].images







# 封面图片
def down_cover(al, c_img):
    for down_cover_al in al:
        # 封面图片
        cover_url = down_cover_al['video']['cover']['url_list'][0]
        cover_res = requests.get(cover_url, headers=headers)
        with open(f'cover/cover-{c_img}.jpeg', 'wb') as f:
            f.write(cover_res.content)
        # pass










# 视频
def down_video(al, c):
    for down_video_al in al:
        # 视频
        video_url = down_video_al['video']['play_addr']['url_list'][-1]
        print('======================================')
        print('视频链接：')
        print(video_url)
        print('======================================')
        video_res = requests.get(video_url, headers=headers)
        with open(f'video/{c}.mp4', 'wb') as f:
            f.write(video_res.content)
        print(f'{c}.mp4，下载完成')
        c += 1
        # pass













# 视频 图片组合作品
def down_img_video(al, iv):
    # 多个视频 + 多个图片组合作品，图片
    print('视频 + 图片组合作品')
    try:
        for images_iv in al:
            print('======================================')
            # 文案
            desc_iv = images_iv['desc']
            print('文案：')
            print(desc_iv)
            print('======================================')
            images_urls_iv = images_iv['images']
            for iui in images_urls_iv:
                images_url_i = iui['url_list'][-1]
                print('======================================')
                print('图片链接：')
                print(images_url_i)
                print('======================================')
                images_res_i = requests.get(images_url_i, headers=headers)
                with open(f'video_img/{iv}.jpeg', 'wb') as f:
                    f.write(images_res_i.content)
                print(f'{iv}.jpeg，图片下载完成')
                iv += 1
    except:
        print('没有视频 + 图片组合作品，图片')
        # pass
    # 短视频
    # aweme_list[19].images[0].video.play_addr.url_list
    # images_video_url_c_s
    # 视频 + 图片组合作品，视频
    try:
        for images_iv in al:
            images_list_iv = images_iv['images']
            for image_iv in images_list_iv:
                # 封面图片
                cover_video_iv = image_iv['video']['cover']['url_list'][0]
                print('======================================')
                print('封面图片链接：')
                print(cover_video_iv)
                print('======================================')
                cover_res_iv = requests.get(cover_video_iv, headers=headers)
                with open(f'video_img/cover-{iv}.jpeg', 'wb') as f:
                    f.write(cover_res_iv.content)
                print(f'cover-{iv}.jpeg，封面图片下载完成')
                iv += 1
                # 视频
                video_url_iv = image_iv['video']['play_addr']['url_list'][-1]
                print('======================================')
                print('视频链接：')
                print(video_url_iv)
                print('======================================')
                video_res_iv = requests.get(video_url_iv, headers=headers)
                with open(f'video_img/{iv}.mp4', 'wb') as f:
                    f.write(video_res_iv.content)
                print(f'{iv}.mp4，视频下载完成')
                iv += 1
    except:
        print('没有视频 + 图片组合作品，视频')
        # pass















# 文案
def desc(al):
    for desc_al in al:
        # 文案
        desc = desc_al['desc']
        print('======================================')
        print(desc)
        print('======================================')















headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0',
    'referer': 'https://www.douyin.com'
}
# 视频图片组合作品
iv = 1
# 视频
c = 1
# 封面图片
c_img = 1
# 图片
img_c = 1
# 页数
page = 1
# 链接
url = input('输入链接：')
# 启动浏览器
dp = ChromiumPage()
# url = 'https://www.douyin.com/user/MS4wLjABAAAAKmhsjiGavu1iTPBLYKD0-m9P-3LG1cJkNrFhaO-JgrnfTbqrgSw66hrf26se_eHC?showSubTab=video&showTab=post'
# url = 'https://www.douyin.com/user/MS4wLjABAAAAKmhsjiGavu1iTPBLYKD0-m9P-3LG1cJkNrFhaO-JgrnfTbqrgSw66hrf26se_eHC'
dp.listen.start('web/aweme/post')
dp.get(url)
dp.wait.doc_loaded()
while True:
    print('======================================')
    print(f'第{page}页')
    print('======================================')
    dlw = dp.listen.wait()
    js = dlw.response.body
    al = js['aweme_list']


    # 文案
    desc(al)

    # 图片+视频组合作品
    down_img_video(al, iv)

    # 图片
    down_images(al, img_c)

    # 封面图片
    down_cover(al, c_img)

    # 视频
    down_video(al, c)








    # 下拉加载
    dp.scroll.to_see('css:.ayFW3zux')
    page += 1


#

